<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="close" onclick="handleClick">关闭对话</div>
    <script>
        var ws = new WebSocket('ws://localhost:5000');

        ws.onopen = function (evt) {
            console.log("已建立 WebSocket 连接");
            // 发送给服务端
            let message = JSON.stringify({
                message: '你好',
                appid: '1bc3840e51434d7c889e7d48f2702d04'
            })
            ws.send(message);
        };

        ws.onmessage = function (evt) {
            console.log("接收到服务器返回消息为: " + evt.data);
        };
        document.getElementById("close").onclick = function () {
            ws.close();
            console.log("关闭");
        }
        ws.onclose = function (evt) {
            console.log("关闭WebSocket连接。");
        }; 
    </script>
</body>

</html>